package com.demo.chapter12date;

import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;

//含有年、月、日
public class LocalDateDemo {

	public static void demo(){
		//LocalDateDemo实例是不可变的，只有日期，不含时间信息
		LocalDate date = LocalDate.of(2016, 9, 18);
		System.out.println(date.getYear());
		System.out.println(date.getMonth());
		System.out.println(date.getDayOfMonth());
		System.out.println(date.getDayOfWeek());
		//这个月份有几天
		System.out.println(date.lengthOfMonth());
		//是否闰年
		System.out.println(date.isLeapYear());
		//获取当前日期
		LocalDate today = LocalDate.now();

		LocalDate date2 = LocalDate.parse("2016-09-18");
	}

	//修改，只会生成新的实例
	public static void demo2(){
		//绝对方式修改
		LocalDate date = LocalDate.of(2016, 9, 18);
		LocalDate date2 = date.withYear(2011);//2011-9-18
		LocalDate date3 = date2.withDayOfMonth(25);//2011-9-25
		LocalDate date4 = date3.with(ChronoField.MONTH_OF_YEAR, 2);//2011-2-25
		System.out.println(date.getYear() + "," + date.getMonth() + "," + date.getDayOfMonth());
		System.out.println(date2.getYear() + "," + date2.getMonth() + "," + date2.getDayOfMonth());
		System.out.println(date3.getYear() + "," + date3.getMonth() + "," + date3.getDayOfMonth());
		System.out.println(date4.getYear() + "," + date4.getMonth() + "," + date4.getDayOfMonth());
	}

	public static void demo3(){
		//相对方式修改
		LocalDate date = LocalDate.of(2016, 9, 18);
		LocalDate date2 = date.plusWeeks(1);//2016-9-25
		LocalDate date3 = date2.minusYears(3);//2013-9-25
		LocalDate date4 = date3.plus(6, ChronoUnit.MONTHS);//2014-3-25
		System.out.println(date.getYear() + "," + date.getMonth() + "," + date.getDayOfMonth());
		System.out.println(date2.getYear() + "," + date2.getMonth() + "," + date2.getDayOfMonth());
		System.out.println(date3.getYear() + "," + date3.getMonth() + "," + date3.getDayOfMonth());
		System.out.println(date4.getYear() + "," + date4.getMonth() + "," + date4.getDayOfMonth());
	}

	public static void main(String[] args) {
		demo();
		System.out.println("===========");
		demo2();
		System.out.println("===========");
		demo3();
	}

}
